package cn.net.yto.school.service.impl;

import cn.net.yto.base.dao.pagination.PaginationRequest;
import cn.net.yto.base.dao.pagination.PaginationResponse;
import cn.net.yto.school.dao.dto.ClazzStudentCountStsDto;
import cn.net.yto.school.dao.entities.TbClazz;
import cn.net.yto.school.dao.repositories.TbClazzRepository;
import cn.net.yto.school.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private TbClazzRepository clazzRepository;

//    @Autowired
//    private TAccountRepository accountRepository;

    @Override
    public List<TbClazz> findAllClazz() {
        return clazzRepository.findAll();
    }

    @Override
    public PaginationResponse<TbClazz> findAllClazzByPagination(PaginationRequest paginationRequest) {

        /*  反例代码
        List<TbClazz> clazzList =  this.clazzRepository.findAll();

        PageRequest pageRequest = new PageRequest(paginationRequest.getCurrentPage() - 1, paginationRequest.getPageSize());


        Long count = this.clazzRepository.count();
        Page<TbClazz> page = new PageImpl<>(clazzList, pageRequest, count);
        PaginationResponse<TbClazz> paginationResponse = new PaginationResponse<TbClazz>(page, TbClazz.class);

        paginationResponse.setContent(page.getContent());
        paginationResponse.setCurrentPage(paginationResponse.getCurrentPage() + 1);

        return paginationResponse;
        */
        PaginationResponse<TbClazz> plist = this.clazzRepository.findResultsByPagination(paginationRequest);

        return plist;
    }

    @Override
    @Transactional
    public void saveClazz(TbClazz tbClazz) {
        this.clazzRepository.save(tbClazz);
    }

    @Override
    @Transactional
    public void deleteClazz(Integer clsId) {
        this.clazzRepository.delete(clsId);
    }

    @Override
    public TbClazz findOneByClazzId(Integer clsId) {
        return this.clazzRepository.findOne(clsId);
    }

    @Override
    public Long countClazzes() {
        return this.clazzRepository.count();
    }

    @Override
    public List<ClazzStudentCountStsDto> countClazzStudent() {
        return this.clazzRepository.countClazzStudent();
    }
}
